package com.example.knowledgeget1.Util;

import com.example.knowledgeget1.GraphDBCommon.Entity.Node;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RelationResponseLexer {
    public static void Lexer(String nodeName, String jsCode, ArrayList<String> list) throws IOException {
        boolean foundText = false;
        String text = "";

        String stringsPattern = "\"[^\"\\\\]*(\\\\.[^\"\\\\]*)*\"";

        // 组合规则
        Pattern pattern = Pattern.compile(
                String.join("|", new String[]{
                        stringsPattern,
                })
        );

        Matcher matcher = pattern.matcher(jsCode);

        while (matcher.find()) {
            if (matcher.group().matches(stringsPattern)) {
                if (foundText) {
                    // 简单处理text文本
                    text = matcher.group();
                    text = text.replace("\\n", "\n");
                    text = text.substring(1, text.length() - 1);
                    text = text.replaceAll("\\\\", "");
//                    System.out.println(text);

                    // 如果文本中第一个字是‘无’，则不建立任何关系，否则将建立关系
                    if (!text.startsWith("无")) {
                        String[] parsedItems = text.split("/");
                        parsedItems = Arrays.stream(parsedItems).filter(item -> !item.equals(nodeName)).toArray(String[]::new);
//                        System.out.println(Arrays.toString(parsedItems));
                        for(String prenodename : parsedItems){
                            if(!list.contains(prenodename)){
                                continue;
                            }
                            String url = String.format("http://localhost:8080/nodes/savePre/%s/%s", prenodename, nodeName);
//                            System.out.println(url);
                            RestTemplate restTemplate = new RestTemplate();
                            HttpHeaders headers = new HttpHeaders();
                            headers.setContentType(MediaType.APPLICATION_JSON);
                            HttpEntity<Node> entity = new HttpEntity<>(headers);
                            ResponseEntity<Node> response = restTemplate.postForEntity(url, entity, Node.class);
                            // 处理响应
                            if (response.getStatusCode().is2xxSuccessful()) {
                                System.out.println("Node relation created successfully");
                            } else {
                                System.err.println("Failed to create node relation. Status code: " + response.getStatusCode());
                            }
                        }
                    }
                    foundText = false;
                }
                if (matcher.group().contains("text")) {
                    foundText = true;
                }
            }
        }
    }

}
